<?php
namespace User\Controller;
use Think\Controller;
/**
 * 提现管理
 */
class WithdrawController extends CkController {
    
    //提现日志
    public function index(){
       $user=$this->userinfo;
       //$data['log']=M('tixian')->where("uid=".$user['id'])->order("id desc")->select();
       $this->assign($data);
       $this->display();
    }
    
    //提现日志AJAX返回
    public function tixian_log()
    {
       $user=$this->userinfo;
       $time = time()-(3600*24);
       $page =(int) I("get.page");
       $M=M('tixian');
       $count = $M->where("uid=".$user['id'])->count(1);
       $page_size=20;
       $page_total_num = ceil($count/$page_size);
       $page_start = $page * $page_size;
       $data=array(
            "total_num" => $count,
            "page_size" => $page_size,
            "page_total_num" => $page_total_num,
        );
       $data['list']=$M->where("uid=".$user['id'])->limit($page_start,$page_size)->order("id desc")->select(); 
       $this->ajaxReturn($data);
    }
    

    //申请提现
    public function apply()
    {  $data['user']=$this->userinfo;
       $this->assign($data);
       $this->display();
    }
    
    //提现处理
    public function apply_do()
    { 
      $user=$this->userinfo;
      $data= I('post.');
      $data['price'] = (int)$data['price'];
      $rules = array(
      array('alipay','require','支付宝账号不可以为空.'), 
      array('alipay','/(^(13[0-9]|15[0-9]|18[0-9]|14[0-9]|17[0-9])[0-9]{8}$)|(^([\w-\.]+)@([\w-]+\.)+([a-zA-Z]{2,4})$)/','支付宝账号不正确.'), 
      array('alipayname','require','支付宝姓名不可以为空'),
      //array('alipayname','/^[0-9A-Z_a-z\u0391-\uFFE5]*$/','支付宝姓名不正确.'),
      array('price','require','提现金额不可以为空.'),
      );
      $M=M("user");
      if (!$M->validate($rules)->create()){
         $this->error($M->getError());
      }
      if($data['price'] < C('TIXIAN_MONEY')){
         $this->error("对不起,提现金额最低".C('TIXIAN_MONEY')."元!");  
      }
      if($user['balance']<$data['price']){
        $this->error("对不起,您的余额不足.");
      }

	  $t = time();
	  $s = strtotime(date('Y-m-d',$t));
	  $tixian = M('tixian');
	  $w = array(
		'time' => array(array('lt',$t),array('gt',$s)),
		'uid' => $user['id'],
	  );
	  $r = $tixian->where($w)->find();
	  if($r){
	     $this->error("对不起,每天只能提现一次!");  
	  }

      //开启事务
      $M->startTrans();

      $delBalance =$M->lock(true)->where("id =".$user['id'])->setDec("balance",$data['price']);

      //看一下会员是否没有绑定支付宝
      if ($user['alipay']!=='0' && $user['alipayname']!=='0') {
         if($user['alipay']!==$data['aliapy'] && $user['alipayname']!==$data['alipayname']){
            $this->error("提现支付宝账号和姓名和您绑定支付宝账号和姓名不一致!");
         }
      }else{
         $AlipayData = array(
               'alipay'    =>$data['alipay'],
               'alipayname'=>$data['alipayname']
            );
         $M->lock(true)->where("id =".$user['id'])->save($AlipayData);
      }
      //添加提现日志
      $data2=array(
         'uid'=>$user['id'],
         'price'=>$data['price'],
         'time'=>time(),
         'type'=>1, //类型 1 支付宝 2 手机充值 
         'status'=> 1, // // 1:审核中  2:拒绝处理  3:处理失败 
        );
      $tixian_log=M("tixian")->add($data2);
      if($delBalance && $tixian_log){
         $M->commit(); //事务提交
         $this->success("申请成功!一个工作日内到账.");
      }else{
         $M->rollback(); //回滚
         $this->success("处理失败.请稍等在操作.");
      }

    }



}